<template>
  <a-card :bordered="false" destroyOnClose>
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="告警级别">
              <a-input v-model="queryParam.alarmLevel" placeholder="请输入告警级别"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="需要处理">
              <j-switch v-model="queryParam.isNeedDeal" placeholder="请选择需要处理" query></j-switch>
            </a-form-item>
          </a-col>
          <a-col v-show="toggleSearchStatus" :lg="14" :md="14" :sm="24" :xl="14">
            <a-form-item label="创建日期">
              <j-date v-model="queryParam.createTime_begin" :show-time="true" class="query-group-cust" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date v-model="queryParam.createTime_end" :show-time="true" class="query-group-cust" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间"></j-date>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <span class="table-page-search-submitButtons" style="float: left;overflow: hidden;">
              <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
              <a-button icon="reload" style="margin-left: 8px" type="primary" @click="searchReset">重置</a-button>
              <a style="margin-left: 8px" @click="handleToggleSearch">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="ipagination"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :scroll="{x:true}"
        bordered
        class="j-table-force-nowrap"
        rowKey="id"
        size="middle"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>

        <template slot="alarmLevelSlots" slot-scope="text, record">
          <a-tag v-if="alarmLevelOptions[text]" :color="alarmLevelOptions[text].color">{{ alarmLevelOptions[text].text }}</a-tag>
        </template>

        <template slot="needDealSlots" slot-scope="text, record">
          <a-icon
            :style="{'color':record.isNeedDeal?'red':'green'}"
            :type="record.isNeedDeal?'tool':'check-circle'"
            theme="filled"/>
          {{ record.isNeedDeal ? '需要处理' : '无需处理' }}
        </template>

        <span slot="action" slot-scope="text, record">
          <template v-if="record.isNeedDeal">
            <a @click="handleDeal(record.id)">处理</a>
            <a-divider type="vertical"/>
          </template>
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>
    <AlarmReportModal ref="modalForm" @ok="modalFormOk"></AlarmReportModal>
    <AlarmDealModel ref="alarmDealModel" @ok="modalFormOk"></AlarmDealModel>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import JDate from '@/components/jeecg/JDate.vue'
import JSwitch from '@/components/jeecg/JSwitch'
import JSearchSelectTag from '@comp/dict/JSearchSelectTag'
import AlarmReportModal from '@views/iotcp/device/alarm/report/AlarmReportModal'
import {getAction} from '@api/manage'
import AlarmDealModel from '@views/iotcp/device/alarm/report/AlarmDealModel'

export default {
  name: 'AlarmRecordList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    AlarmDealModel,
    JSearchSelectTag,
    JDictSelectTag,
    JDate,
    JSwitch,
    AlarmReportModal
  },
  props: {
    instanceId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // description: '告警记录管理页面',
      alarmLevelOptions: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '实例',
          align: 'center',
          dataIndex: 'instanceId_dictText'
        },
        {
          title: '告警配置',
          align: 'center',
          dataIndex: 'alarmConfigId_dictText'
        },
        {
          title: '告警级别',
          align: 'center',
          dataIndex: 'alarmLevel',
          scopedSlots: {customRender: 'alarmLevelSlots'}
        },
        {
          title: '需要处理',
          align: 'center',
          dataIndex: 'isNeedDeal',
          scopedSlots: {customRender: 'needDealSlots'}
        },
        {
          title: '创建日期',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      queryParam: {instanceId: this.instanceId},

      url: {
        list: '/alarm/record/list',
        delete: '/alarm/record/delete',
        deleteBatch: '/alarm/record/deleteBatch',
        exportXlsUrl: '/alarm/record/exportXls',
        importExcelUrl: 'alarm/record/importExcel'
      },
      dictOptions: {}
    }
  },
  created() {
    this.$set(this.dictOptions, 'isNeedDeal', [{text: '是', value: 'Y'}, {text: '否', value: 'N'}])
  },
  mounted() {
    this.getAlarmLevel()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    getAlarmLevel() {
      let scope = this
      getAction('alarm/config/getAlarmLevel').then(data => {
        if (data.success) {
          scope.alarmLevelOptions = data.result
        }
      })
    },
    handleDeal(record) {
      this.$refs.alarmDealModel.deal(record)
    },
    handleDetail(record) {
      this.$refs.modalForm.disableSubmit = true
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '详情'
    },
    searchReset() {
      this.queryParam = {instanceId: this.instanceId}
      this.loadData(1)
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>